/**
 * Created by LeungZ on 2016/10/17.
 */
class Deployment {
    constructor($scope, ctgNotification, nodeSrv, appSrv, $state, appImgList) {
        "ngInject";
        $scope.$parent.vm.step = 2;
        this._notification = ctgNotification;
        this.serviceList = [];
        this._fData = $scope.$parent.vm.fData;
        this._appSrv = appSrv;
        this._state = $state;

        if (appImgList) {
            this.appImgList = appImgList.list;
        }
        this.groupList = [];

        if (this._fData.cluster) {
            nodeSrv.getGroupList({ perPage: 99, page: 1, code: this._fData.cluster.clusterCode })
                .then(data => {
                    this.groupList = data.list;
                    angular.forEach(this.serviceList, service => {
                        service.groupList = this.groupList;
                    });
                }).catch(data => {
                    this._notification.error('获取宿主机组详情数据有误');
                })
        }
        if (!appSrv._cpulimit){
            this.CpuInfo();
        }
        if (this._fData.hasOwnProperty('services')) {
            this.serviceList = this._fData.services;
        } else this.addService();
    }

    CpuInfo(){
        this._appSrv.getConfig().then(data=>{
        this._appSrv._logPath = data['log.hostPath'];
        this._appSrv._cpuReq = data['cpu.request'];
        this._appSrv._memReq = data['memory.request'];
        this._appSrv._memUnit = data['memory.unit'];
        this._appSrv._dfPort  = data['deployment.externalPort'];
        this._appSrv._memorylimit = Number(data['memory.limit']);
        this._appSrv._cpulimit = Number(data['cpu.limit']);
        }).catch(data=>{
            this._notification.error('cpu数据有误');
        })
    }
    addService() {
        this.serviceList.push({
            opened: false,
            currentForm: 'vol',  //资源配置
            appImgList: this.appImgList,
            groupList: this.groupList,
            checkServiceValidate: this.checkServiceValidate.bind(this),
            podCpu: this._appSrv._cpulimit,
            podMem: this._appSrv._memorylimit,
            podPort: this._appSrv._dfPort
        });
    }

    checkServiceValidate(service) {
        for (var i in this.serviceList) {
            if (this.serviceList[i].podAppName == service.podAppName && service !== this.serviceList[i]) {
                return service.$forms.base.podAppName.$setValidity('duplicate', false);
            }
        }
        service.$forms.base.podAppName.$setValidity('duplicate', true);
    }

    deleteService(index) {
        this.serviceList.splice(index, 1);
    }

    expendForm(elem) {
        if (elem.opened) {
            elem.opened = this._appSrv.checkInvalid(elem.$forms);
        } else {
            elem.opened = true;
        }
    }

    next() {
        // console.log(this.serviceList)
        if (!this.serviceList.length) return;
        for (var i in this.serviceList) {
            if (this._appSrv.checkInvalid(this.serviceList[i].$forms)) {
                return;
            } else {
                this.serviceList[i].opened = false;
            }
             this.serviceList[i].envs.forEach((en,ind)=>{
                 if(en.hide)
                delete en.hide;//去掉隐藏属性
            })
        }

        this.loading = true;
        this._fData.services = this.serviceList;

        this._appSrv.getYaml(this._fData).then(data => {
            this._fData.yJsonList = data;
            this._state.go('app-service-publish.release');
        }).catch((data) => {
            if (data.resultCode == 1200) {
                this._notification.error(data.resultMsg);
            } else {
                this._notification.error("加载Yaml配置失败.");
            }
        }).finally(() => {
            this.loading = false;
        })

    }
}
export default Deployment